﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDSP.BRL;
using LDSP.Util;
using System.Data;
using Syncfusion.XlsIO;

namespace ImplementationInitialization
{
    public class FinalStandardImport : FinalImport
    {
        protected override string AnalyseCodName(string fileName)
        {
            string codName = fileName.IndexOf(".") > -1 ? fileName.Substring(0, fileName.IndexOf(".")) : fileName;

            //倒数第二个下划线之前的是标准号。
            if (fileName.Contains("_"))
            {
                codName = fileName.Substring(0, fileName.LastIndexOf("_"));
            }

            if (codName.Contains("_"))
            {
                codName = codName.Substring(0, codName.LastIndexOf("_"));
            }

            return codName;
        }

        protected override void Import(string filename, DataTable dt, int catalogID, string sheetName)
        {
            Codification cod = null;
            IDictionary<string, Codification> standardDic = BaseDataInitial.GetExistedCodification();
            IDictionary<string, Catalog> catalogDic = BaseDataInitial.GetCatalogs();
            foreach (DataRow row in dt.Rows)
            {
                string referenceNumber = referenceNumber = row[1] != null ? row[1].ToString() : string.Empty;
                referenceNumber = referenceNumber.Trim();
                try
                {
                    Logger.WriteLog("====Begin Import Codification : " + referenceNumber);

                    if (standardDic.ContainsKey(referenceNumber))
                    {
                        cod = standardDic[referenceNumber];
                        SetCodification(cod, catalogID, row, referenceNumber);

                        cod.Update();

                        WriteImportItemResult(referenceNumber, filename, sheetName, "更新", true);
                    }
                    else
                    {
                        cod = new Codification();
                        SetCodification(cod, catalogID, row, referenceNumber);

                        cod.DocType = DocType.标准;

                        //Just mark newly added
                        cod.Comment1 = DateTime.Now.ToString();

                        cod.Add();

                        //加入到缓存.
                        BaseDataInitial.Add(standardDic, cod);
                        _newCodifications.Add(cod);

                        WriteImportItemResult(referenceNumber, filename, sheetName, "添加", true);
                    }
                }
                catch (Exception ex)
                {
                    string message = "Error import : " + "File Name : " + filename + " Codification : " + cod.Name
                        + "\n" + "Message : " + ex.Message;
                    Logger.WriteLog(message);

                    WriteImportItemResult(referenceNumber, filename, sheetName, message, false);
                }

                Logger.WriteLog("====End Import Codification : " + referenceNumber);
            }
        }

        protected override IList<string> GetDisableList(Codification cod)
        {
            return _invalidList[cod].Split('\n').ToList<string>();
        }

        protected override void WriteImportAttachmentResult(string fileName, string message, bool success)
        {
            ImportResult.AddAttachmentResult(fileName, DocType.标准, message, success);
        }

        protected override void WriteImportItemResult(string codName, string excel, string sheet, string message, bool success)
        {
            ImportResult.AddItemResult(excel, DocType.标准, sheet, codName, message, success);
        }

        private Codification SetCodification(Codification cod, int catalogID, DataRow row, string referenceNumber)
        {
            int i_category = 0;
            int i_name = 2;
            int i_organizaiton = 3;
            int i_issue_date = 4;
            int i_implementation_date = 5;
            int i_summary = 6;
            int i_implementation_methods = 7;
            int i_invalid_list = 8;

            //类别
            cod.CategoryID = row[i_category] != null && _categoryDic.ContainsKey(row[i_category].ToString()) ? _categoryDic[row[i_category].ToString()].ID : 0;

            //法律法规名称
            string name = row[i_name] != null ? row[i_name].ToString() : string.Empty;
            cod.Name = GetChineseName(name);

            //发布机构
            cod.Organization = row[i_organizaiton] != null ? row[i_organizaiton].ToString() : string.Empty;

            //发布文号
            cod.ReferenceNumber = referenceNumber;

            //发布日期
            DateTime releaseDT = DateTime.MinValue;
            if (row[i_issue_date] != null && DateTime.TryParse(row[i_issue_date].ToString(), out releaseDT))
            {
                releaseDT = DateTime.Parse(row[i_issue_date].ToString());
            }
            if (releaseDT != DateTime.MinValue)
            {
                cod.ReleaseDT = releaseDT;
            }

            //实施日期
            DateTime implementDT = DateTime.MinValue;
            if (row[i_implementation_date] != null && DateTime.TryParse(row[i_implementation_date].ToString(), out implementDT))
            {
                implementDT = DateTime.Parse(row[i_implementation_date].ToString());
            }
            if (implementDT != DateTime.MinValue)
            {
                cod.ImplementDT = implementDT;
            }

            cod.CatalogID = catalogID;

            //法规概述
            cod.Summary = row[i_summary] != null ? row[i_summary].ToString() : string.Empty;

            //实施措施
            //cod.Action = row[i_implementation_methods] != null ? row[i_implementation_methods].ToString() : string.Empty;

            //废止，修改的标准
            _invalidList.Add(cod, row[i_invalid_list] != null ? row[i_invalid_list].ToString() : string.Empty);
            cod.InvalidDescription = row[i_invalid_list] != null ? row[i_invalid_list].ToString() : string.Empty;

            //更新时间
            cod.UploadDT = this.UploadDT;

            return cod;
        }
    }
}
